﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Space_Conquest
{

    enum BuildingCreationQueueType
    {
        NotCreatingUnit,
        Laborer,
        Battleship,
        Jeep,
        LightShip,
        Robot,
        Soldier,
        Tank,
        Transporter
    }

    abstract class BuildingUnit : Unit
    {

        #region Private Members

        protected static Texture _foundation1 = new Texture("Images/BuildingFoundation1.bmp");
        protected static Texture _foundation2 = new Texture("Images/BuildingFoundation2.bmp");

        protected static Texture _buildBar = new Texture("Images/BuildBar.bmp");
        protected static Texture _createBar = new Texture("Images/CreateBar.bmp");

        protected List<BuildingCreationQueueType> _creationQueue = new List<BuildingCreationQueueType>();
        protected int _currentCreationTime;

        protected int _currentConstructionTime;
        protected bool _isBuilt;
        protected int _necessaryBuildTime;

        #endregion

        #region Properties

        public virtual int CreationPercentFinished
        {
            get
            {
                return 0;
            }
        }

        public virtual int BuiltPercentFinished
        {
            get
            {
                return (int)(((double)_currentConstructionTime / (double)_necessaryBuildTime) * 100);
            }
        }

        public bool IsBuilt
        {
            get
            {
                return _isBuilt;
            }
            set
            {
                _isBuilt = value;
            }
        }

        public bool IsCreatingUnit
        {
            get
            {
                return (_creationQueue.Count > 0);
            }
        }

        public BuildingCreationQueueType UnitTypeCurrentlyCreating
        {
            get
            {
                if (IsCreatingUnit)
                    return _creationQueue[0];
                else
                    return BuildingCreationQueueType.NotCreatingUnit;
            }
        }

        #endregion

        #region Constructor

        public BuildingUnit(Texture texture, int x, int y, Player owner)
            : base(texture, x, y, owner)
        {

            _isBuilt = true;
            _currentCreationTime = 0;

        } 

        #endregion

        #region Methods

        public override void Motion()
        {

        }

        public override void Render()
        {

            if (IsBuilt)
            {
                _sprite.Render(_texture);
            }
            else
            {
                if (BuiltPercentFinished < 50)
                {
                    _sprite.Render(_foundation1);
                }
                else
                {
                    _sprite.Render(_foundation2);
                }
            }

            RenderLifeBar();

            if (!_isBuilt)
                RenderBuildBar();
            else if (_creationQueue.Count > 0)
                RenderCreateBar();

        }

        public void CreateUnit(BuildingCreationQueueType type)
        {
            ValidateType(type);
            _creationQueue.Add(type);
        }

        void ValidateType(BuildingCreationQueueType type)
        {

            if (this is TownCenter)
            {
                switch (type)
                {
                    case BuildingCreationQueueType.Laborer:
                        return;
                    default:
                        throw new Exception("Error in CreateUnit, TownCenter can't make: " + type.ToString());
                }
            }
            else if (this is Barrack)
            {
                switch (type)
                {
                    case BuildingCreationQueueType.Soldier:
                    case BuildingCreationQueueType.Robot:
                        return;
                    default:
                        throw new Exception("Error in CreateUnit, Barrack can't make: " + type.ToString());
                }
            }
            else if (this is Factory)
            {
                switch (type)
                {
                    case BuildingCreationQueueType.Jeep:
                    case BuildingCreationQueueType.Tank:
                        return;
                    default:
                        throw new Exception("Error in CreateUnit, Factory can't make: " + type.ToString());
                }
            }
            else if (this is AirStation)
            {

                switch (type)
                {
                    case BuildingCreationQueueType.LightShip:
                    case BuildingCreationQueueType.Transporter:
                    case BuildingCreationQueueType.Battleship:
                        return;
                    default:
                        throw new Exception("Error in CreateUnit, AirStation can't make: " + type.ToString());
                }

            }
            else
            {
                throw new Exception("Error in CreateUnit, Building can't create units: " + this.GetType().Name);
            }

        }

        public virtual void BuildTick()
        {

            _currentConstructionTime++;

            if (_currentConstructionTime >= _necessaryBuildTime)
            {
                _isBuilt = true;
            }

            if (_currentConstructionTime % 20 == 0)
            {

                Parent.Parent.LocalPlayer.PlaySound("Sounds/Build.wav", MapPoint);

                List<MapPoint> points = MapArea.GetListOfAllMapPoints();
                Parent.Parent.AnimationManager.DisplayAnimation(AnimationType.BuildHammer,
                                                                RenderLayer.AboveGroundUnitsBelowAirUnits,
                                                                points[new Random().Next(0, points.Count)]);

            }

        }

        protected void RenderBuildBar()
        {

            _barSprite.Alpha = 150;
            _barSprite.Left = _sprite.Left; _barSprite.Top = _sprite.Top + 9;
            _barSprite.Height = 4;
            _barSprite.Width = (int)((double)(_sprite.Width) * ((double)BuiltPercentFinished)/100);
            if (_barSprite.Width <= 0)
                _barSprite.Width = 1;
            _barSprite.Render(_buildBar);

            _barSprite.Alpha = 255;
            _barSprite.Top -= 4;
            _barSprite.Width = _sprite.Width; _barSprite.Height = 12;
            _barSprite.Render(_unitBar);

        }

        protected void RenderCreateBar()
        {

            _barSprite.Alpha = 200;
            _barSprite.Left = _sprite.Left; _barSprite.Top = _sprite.Top + 9;
            _barSprite.Height = 4;
            _barSprite.Width = (int)((double)(_sprite.Width) * ((double)CreationPercentFinished) / 100);
            if (_barSprite.Width <= 0)
                _barSprite.Width = 1;
            _barSprite.Render(_createBar);

            _barSprite.Alpha = 255;
            _barSprite.Top -= 4;
            _barSprite.Width = _sprite.Width; _barSprite.Height = 12;
            _barSprite.Render(_unitBar);

        }

        #endregion

    }
}
